package com.hxzy.facade.controller.auth;

import com.hxzy.framework.common.constant.AuthConstant;
import com.hxzy.framework.common.constant.RedisConstant;
import com.hxzy.framework.common.exception.ApiException;
import com.hxzy.framework.common.req.CaptchaCodeReq;
import com.hxzy.framework.common.req.LoginReq;
import com.hxzy.framework.common.resp.CaptchaCodeResp;
import com.hxzy.framework.common.resp.LoginResp;
import com.hxzy.framework.common.resp.R;
import com.hxzy.framework.common.util.ResultUtil;
import com.hxzy.module.auth.api.auth.AuthFeignApi;
import com.hxzy.module.auth.api.auth.resp.Oauth2TokenDto;
import com.hxzy.module.merchant.api.merchant.MerchantFeignApi;
import com.hxzy.module.merchant.api.merchant.resp.MerchantResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author wy
 * @Description TODO
 * @Date 2024/1/26 16:48
 * @Version 1.0
 */
@RestController
@Api(tags = "总店后台-登录授权接口")
@Log4j2
@RequestMapping("/auth")
public class AuthController {

    /**
     * 统一认证远程服务API
     */
    @Autowired
    private AuthFeignApi authFeignApi;

    /**
     * 商店远程服务API
     */
    @Autowired
    private MerchantFeignApi merchantFeignApi;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping(value = "/rsa/publicKey")
    public Map<String, Object> getKey(){
        return authFeignApi.getKey();
    }

    /**
     * 获取验证码
     * @param req
     * @return
     */
    @ApiOperation(value ="easyCaptcha验证码")
    @GetMapping("/captchaCode")
    public R<CaptchaCodeResp> captcha( CaptchaCodeReq req){
       return this.authFeignApi.easyCaptcha(req);
    }


    /**
     * 后台管理会员登录
     *
     * @param req
     * @return
     */
    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public R<LoginResp> login(@RequestBody @Valid LoginReq req) {
       //验证码
        String redisKey= RedisConstant.CAPTCHA_KEY +req.getCaptchaNo();
        Object redisValue = this.redisTemplate.opsForValue().get(redisKey);
        if(Objects.isNull(redisValue)){
            throw new ApiException(500,"验证码已过期!");
        }

        this.redisTemplate.delete(redisKey);

        if(!req.getCaptchaCode().equalsIgnoreCase(redisValue.toString())){
            throw new ApiException(500,"验证码错误!");
        }


        HashMap<String,String> parameters =new HashMap<>();
        parameters.put("grant_type","password");
        parameters.put("client_id", AuthConstant.ADMIN_CLIENT_ID);
        parameters.put("client_secret",AuthConstant.ADMIN_CLIENT_SECRET);
        parameters.put("username",req.getAccount());
        parameters.put("password",req.getPassword());

        R<Oauth2TokenDto> oauth2TokenDtoR = this.authFeignApi.postAccessToken(parameters);
        Oauth2TokenDto oauth2TokenDto = ResultUtil.remoteResult(oauth2TokenDtoR);

        LoginResp result=new LoginResp();
        result.setToken(oauth2TokenDto.getToken());
        result.setExpiresIn(oauth2TokenDto.getExpiresIn());

        return R.success(result);
    }

    @ApiOperation(value = "商户信息详情")
    @PostMapping("/merchantDetail")
    public R<MerchantResp> getMerchantDetail(){
        return this.merchantFeignApi.getMerchantDetail();
    }

}
